package com.bwie.shop.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.bwie.common.constants.TokenConstants;
import com.bwie.common.domain.Admin;
import com.bwie.common.domain.Contract;
import com.bwie.common.domain.Shop;
import com.bwie.common.utils.JwtUtils;
import com.bwie.shop.mapper.ShopMapper;
import com.bwie.shop.remote.ShopRemote;
import com.bwie.shop.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class ShopServiceImpl implements ShopService {

    @Resource
    private ShopMapper shopMapper;

    @Autowired
    private ShopRemote shopRemote;

    /**
     * 查询店铺数量合同
     * @return
     */
    @Override
    public List<Shop> selectShopQuantity() {

        List<Shop> shopList = shopMapper.selectShopQuantity();
        List<Contract> contractList = shopRemote.selectContractQuantity().getData();
        for (Shop shop : shopList) {
            String shopName = shop.getShopName();
            for (Contract contract : contractList) {
                String contractShopName = contract.getContractShopName();
                if (shopName.equals(contractShopName)){
                    shop.setShopContractQuantity(contract.getContractQuantity());
                }
            }
        }

        return shopList;
    }


    /**
     * 查询店铺
     * @return
     */
    @Override
    public List<Shop> queryShop() {
        List<Shop> shops = shopMapper.queryShop();
        if (shops == null){
            throw new RuntimeException("查询店铺失败");
        }
        return shops;
    }


    @Autowired
    private HttpServletRequest httpServletRequest;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 添加店铺
     * @param shop
     * @return
     */
    @Override
    public Integer saveShop(Shop shop) {

        Admin info = info();

        shop.setStoreManager(info.getAdminName());
        shop.setShopAdminId(info.getAdminId());
        shop.setShopAdminId(info.getAdminId());
        if (shop.getShopAdminId().equals(info.getAdminId())){
            info.setAdminPrice(info.getAdminPrice() - 500);
        }

        Integer i = shopMapper.saveShop(shop);
        if (i == 0){
            throw new RuntimeException("添加店铺失败");
        }
        return i;
    }



    /**
     * 获取当前登录用户信息
     * @return
     */
    private Admin info(){
        String token = httpServletRequest.getHeader(TokenConstants.TOKEN);
        String userKey = JwtUtils.getUserKey(token);
        String admin = redisTemplate.opsForValue().get(TokenConstants.LOGIN_TOKEN_KEY + userKey);
        Admin admin1 = JSONObject.parseObject(admin, Admin.class);
        return admin1;
    }


    /**
     * 管理员审核店铺
     * @param shop
     * @return
     */
    @Override
    public Integer examine(Shop shop) {
        if (shop.getShopFlag() == 1 && info().getAdminId() == 1){
            Integer examine = shopMapper.examine(shop);
            if (examine == 0){
                throw new RuntimeException("审核店铺失败");
            }
        }
        return null;
    }

    @Override
    public List<Shop> queryContractQuantity() {
        List<Shop> shops = shopMapper.queryContractQuantity();
        return shops;
    }
}
